package com.bbs.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bbs.bean.Topic;
import com.bbs.bean.UserInfo;
import com.bbs.util.MysqlDB;

/** 
 * @className: ControllerSearch.java
 * @classDescription: 
 * @function: 
 * @author: Wentasy
 * @createTime: 2012-11-28 下午02:54:55
 * @modifyTime: 
 * @modifyReason: 
 * @since: JDK 1.6
 */
public class ControllerSearch extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public ControllerSearch() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request,response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
			request.setCharacterEncoding("UTF-8");
			String value = request.getParameter("search");
			String backNews = ""; // 帖子的信息
			Topic topic = new Topic();
			request.setAttribute("topic", topic);
			ResultSet rs = null;
			Connection conn = null;
			Statement stmt = null;
			ArrayList<Topic> topicList = new ArrayList<Topic>();
			
			try {
				conn = MysqlDB.getConnection();
				stmt = MysqlDB.createStatement(conn);
				String sql = "";
				sql = "select * from topic where TopicContent like '%"+value+"%'";
				
				
				rs = stmt.executeQuery(sql);
				
				while(rs.next()){
					backNews = "操作成功";
					topic.setBackNews("成功");
					topic.setTopicId(rs.getInt("topicId"));
					topic.setTopicTitle(rs.getString("topicTitle"));
					topic.setTopicContent(rs.getString("topicContent"));
					topic.setView(rs.getInt("view"));
					topic.setLocked(rs.getInt("locked"));
					topic.setTopicTime(rs.getDate("topicTime").toString());
					topic.setUserId(rs.getInt("userId"));
					topicList.add(topic);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}finally{
				MysqlDB.close(rs);
				MysqlDB.close(stmt);
				MysqlDB.close(conn);
			}
			
			System.out.println(value);
			
			request.setAttribute("topicList", topicList);
			RequestDispatcher dispatcher = request
			.getRequestDispatcher("pages/searchContent.jsp");
			dispatcher.forward(request, response);
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
